﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SRD.Model.SRD.DTO;
using SRD.Model.SRD.BO;
using System.Data;

namespace SRD.View
{
    public partial class editCargo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["id_cargo"] != null)
                {
                    preencherCampos();
                }
            }
        }

        public void preencherCampos()
        {

            CargoDTO cargo = new CargoDTO();

            cargo.CargoId = Convert.ToInt32(Request.QueryString["id_cargo"].ToString());

            DataSet dts = new CargoBO().getCargo(cargo);
            TextBoxNome.Text = dts.Tables[0].Rows[0]["NM_CARGO"].ToString();
            if (dts.Tables[0].Rows[0]["ATIVO"].ToString().Equals("S"))
            {
                CheckBoxAtivo.Checked = true;
            }
            else
            {
                CheckBoxAtivo.Checked = false;
            }
        }

        protected void ButtonVoltar_Click(object sender, EventArgs e)
        {
            
        }

        protected void BtSalvar_Click(object sender, ImageClickEventArgs e)
        {
            if (Request.QueryString["id_cargo"] != null)
            {
                CargoDTO cargo = new CargoDTO();
                cargo.CargoId = Convert.ToInt32(Request.QueryString["id_cargo"].ToString());
                cargo.NomeCargo = TextBoxNome.Text;
                if (CheckBoxAtivo.Checked)
                {
                    cargo.Ativo = 'S';
                }
                else
                {
                    cargo.Ativo = 'N';
                }
                String resposta = new CargoBO().alterarCargo(cargo);
                Session["mensagem"] = resposta;
                //ClientScript.RegisterClientScriptBlock(typeof(string), "alert", "alert('" + resposta + "');", true);
                //ClientScript.RegisterClientScriptBlock(typeof(string), "history", "window.location='listCargo.aspx;", true);
            }
            else
            {
                CargoDTO cargo = new CargoDTO();
                cargo.NomeCargo = TextBoxNome.Text;
                if (CheckBoxAtivo.Checked)
                {
                    cargo.Ativo = 'S';
                }
                else
                {
                    cargo.Ativo = 'N';
                }

                String resposta = new CargoBO().inserirCargo(cargo);
                Session["mensagem"] = resposta;
                //ClientScript.RegisterClientScriptBlock(typeof(string), "alert", "alert('" + resposta + "');", true);
                //ClientScript.RegisterClientScriptBlock(typeof(string), "history", "window.location='listCargo.aspx;", true);
            }
            Response.Redirect("listCargo.aspx");
        }

        protected void BtVoltar_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("listCargo.aspx");
        }
    }
}